import QtQuick 1.0
import "../Util/Util.js" as Util


ModelXmlList {
//    id: _root

    property bool isFirst: true         //最初に1回だけ処理する内容の為のフラグ

    property bool accessReady: true     //上位で読込みに関するReady状態を指定する
    property bool isReady: ready && accessReady    //状態の合成（最終的にこれを見る）

    property bool ready: status == XmlListModel.Ready
    property int pageNumber: 1

    property int sourceType: 0              //取得するデータの種類    0:timeline 1:mention
    property string sourceIds: ""            //Timeline用

    property string lastSelectScreenName: ""    //前回選択のスクリーンネーム
    property string lastSelectMessage: ""       //前回選択のメッセージ
    property string prevUsersUrl: ""         //前回のユーザー情報のURL

    property variant objTwitter: null       // TwitterAPIアクセス用エレメント

    //xmlの解析関係
    query: "/users/user"
    XmlRole { name: "_status_id"; query: "id/string()"; isKey: true}
    XmlRole { name: "_name"; query: "name/string()"; }
    XmlRole { name: "_screen_name"; query: "screen_name/string()"; }
    XmlRole { name: "_text"; query: "status/text/string()"; }
    XmlRole { name: "_icon"; query: "profile_image_url/string()";}
    XmlRole { name: "_date"; query: "created_at/string()"; }
    XmlRole { name: "_in_reply_to_status_id"; query: "status/in_reply_to_status_id/string()"; }
    XmlRole { name: "_in_reply_to_screen_name"; query: "status/in_reply_to_screen_name/string()"; }
    XmlRole { name: "_source"; query: "status/source/string()"; }
    XmlRole { name: "_favorited"; query: "status/favorited/string()"; }

    XmlRole { name: "_bio"; query: "description/string()"; }
    XmlRole { name: "_location"; query: "location/string()"; }
    XmlRole { name: "_followers_count"; query: "followers_count/string()"; }
    XmlRole { name: "_friends_count"; query: "friends_count/string()";}
    XmlRole { name: "_following"; query: "following/string()"; }
    XmlRole { name: "_statuses_count"; query: "statuses_count/string()"; }

    XmlRole { name: "_urls"; query: "entities/urls/string()"; }


    function clearData(){
        //カラデータを指定して無理やり消す
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><users type=\"array\"><user><id /><name /><screen_name /><location /><description /><profile_image_url /><profile_image_url_https /><url /><protected /><followers_count /><profile_background_color /><profile_text_color /><profile_link_color /><profile_sidebar_fill_color /><profile_sidebar_border_color /><friends_count /><created_at /><favourites_count /><utc_offset /><time_zone /><profile_background_image_url /><profile_background_image_url_https /><profile_background_tile /><profile_use_background_image /><notifications /><geo_enabled /><verified /><following /><statuses_count /><lang /><contributors_enabled /><follow_request_sent /><listed_count /><show_all_inline_media /><default_profile /><default_profile_image /><is_translator /><status><created_at /><id /><text /><source /><truncated /><favorited /><in_reply_to_status_id /><in_reply_to_user_id /><in_reply_to_screen_name /><retweet_count /><retweeted></retweeted><retweeted_status /><geo /><coordinates /><place /><contributors /></status></user></users>";
        clear();
    }

    //最新のページを取得する
    function recent() {
        pageNumber = 0;
        if(count < 1){
            //        isAppendTop = true;
            makeTwitterUrl();
        }else if(sourceType === 2){
            //Users
            makeTwitterUrl();
        }else{
        }
    }

    //次のページを取得する
    function more() {
        if (isReady) {
            pageNumber++;
        }else{
        }
        makeTwitterUrl(pageNumber);
    }

    // 応答結果を指定する
    function response(text){
        xml = text;
        accessReady = true;
    }



    //TwitterのURLを取得する
    function makeTwitterUrl(){
        switch(sourceType){
        case 0:
            //Friends
            accessReady = false;
            objTwitter.friendAndFollowers.friendsIds(null, objTwitter.screenName, null, null);
            break;
        case 1:
            //Followers
            accessReady = false;
            objTwitter.friendAndFollowers.followersIds(null, objTwitter.screenName, null, null);
            break;
        case 2:
            //Users
            var names = lastSelectScreenName;
            var pickup = Util.pickUpScreenNames(lastSelectMessage);
            if(pickup === null){
            }else{
                for(var i=0; i<pickup.length; i++){
                    names += "," + pickup[i];
                }
            }

            if(prevUsersUrl != names){
                clearData();
                prevUsersUrl = names;

                accessReady = false;
                objTwitter.friendAndFollowers.friendshipsLookup(names, null, response);
            }

            break;
        default:
            break;
        }
    }

    //アクセスの結果の応答
    function onIdsResponse(responseText){
//        Util.outputDebugString("ModelFriendsFollowers::onIdsResponse:" + responseText);
        //読込み完了

        var re = new RegExp("[0-9]+", "g");
        var ids = responseText.match(re);

        if(ids === null){
//            Util.outputDebugString("ModelFriendsFollowers::onIdsResponse: ids=null");
        }else{
            var id_list = "";
            for(var i=0; i<ids.length; i++){
                if(i >= 100){
                    break;
                }
//                Util.outputDebugString("ModelFriendsFollowers::onIdsResponse: ids[" + i + "]=" + ids[i]);
                if(i !== 0){
                    id_list += ","
                }
                id_list += ids[i];
            }
            objTwitter.friendAndFollowers.friendshipsLookup(null, id_list, response);
        }
    }

}
